package org.feng;

import lombok.extern.slf4j.Slf4j;
import org.feng.manage.sys.AbstractOperateHandlerTemplate;
import org.feng.manage.sys.OperateEnum;
import org.feng.manage.sys.OperateHandlerFactory;
import org.feng.util.SpringBeanUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.lang.NonNull;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;

@Slf4j
@EnableSwagger2
@EnableTransactionManagement
@MapperScan("org.feng.mapper")
@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy = true)
public class FxBackApplication implements ApplicationListener<ApplicationReadyEvent> {

    private static Environment environment;

    public static void main(String[] args) throws UnknownHostException {
        final ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(FxBackApplication.class)
                .bannerMode(Banner.Mode.OFF)
                .run(args);

        Environment env = applicationContext.getEnvironment();
        environment = env;
        String contextPath = env.getProperty("server.servlet.context-path");
        log.info("""

                        ----------------------------------------------------------
                        \tApplication '{}' is running! Access URLs:
                        \tLocal: \t\thttp://localhost:{}{}
                        \tExternal: \thttp://{}:{}{}
                        \tDoc: \thttp://{}:{}{}/doc.html
                        \t----------------------------------------------------------""",
                env.getProperty("spring.application.name"),
                env.getProperty("server.port"), contextPath,
                InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), contextPath,
                InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), contextPath
        );
    }

    @Override
    public void onApplicationEvent(@NonNull ApplicationReadyEvent applicationReadyEvent) {
        String applicationName = "";
        if (environment != null) {
            applicationName = Optional.ofNullable(environment.getProperty("spring.application.name")).orElse("");
        }
        log.info("{}项目启动完成", applicationName);

        for (OperateEnum operateEnum : OperateEnum.values()) {
            Class<? extends AbstractOperateHandlerTemplate<?, ?>> orderHandler = operateEnum.getOperateHandler();
            AbstractOperateHandlerTemplate<?, ?> handlerTemplate = SpringBeanUtils.getByClass(orderHandler);
            OperateHandlerFactory.register(operateEnum, handlerTemplate);
        }
    }

}
